<?php
namespace app\controller;

use app\BaseController;
use app\Request;
use think\facade\Cache;
use think\facade\Session;
use think\facade\View;
use think\facade\Db;

class Login extends BaseController
{
    public function index(Request $request)
    {
        if($request->isAjax()){
            $post = $request->post();
            $user = Db::table('user')->where(['user'=>$post['user']])->find();
            if(empty($user)){
                exit(json_encode(['code'=>0,'msg'=>'用户不存在']));
            }
            if(md5($post['password'])!=$user['password']){
                exit(json_encode(['code'=>0,'msg'=>'密码错误']));
            }
            Cache::set('username',$post['user'],3600);
            Cache::set('password',$post['password'],3600);
            exit(json_encode(['code'=>1,'msg'=>'登录成功','url'=>'/admin']));
        }
        $type = '';
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'Windows')){
            $type ='PC';
        }else{
            $type ='Mobile';
        }
        View::assign('type',$type);
        return View::fetch('index');
    }

    public function hello($name = 'ThinkPHP6')
    {
        return 'hello,' . $name;
    }

    function get_device_type()
    {
        //全部变成小写字母
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        $type = 'other';
        //分别进行判断
        if(strpos($agent, 'iphone') || strpos($agent, 'ipad'))
        {
            $type = 'ios';
        }

        if(strpos($agent, 'android'))
        {
            $type = 'android';
        }
        return $type;
    }
}
